package com.playball.lottery.customview;

import java.util.ArrayList;
import java.util.HashMap;

import com.playball.lottery.R;
//import com.playball.lottery.frm.buyhall.JcfootxhlbActivity;


import com.playball.lottery.frm.buyhall.JcBasketballxhlbActivity;
import com.playball.lottery.frm.buyhall.JcfootxhlbActivity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 竞彩足球购彩半全场，比分，总进球选号列表界面LISTVIEW适配器,和竞彩篮球胜分差公用
 * @author Administrator
 *
 */
public class DelButtonAdapterJCZQBQC extends BaseAdapter {

	private class MyJcFoot {
		TextView hteam;
		TextView vteam;
		TextView touzbu;
		Button buttonClose;
	}

	private ArrayList<HashMap<String, Object>> mAppList;
	private LayoutInflater mInflater;
	private Context mContext;
	private String[] keyString;
	private int[] valueViewID;
	private MyJcFoot myFoot;

	private String str_hteam;
	private String str_vteam;
//	private String str_sp;
	private String str_touzhu;
	/**竞彩足球和篮球胜分差公用*/
	private String ZQWF;

	public DelButtonAdapterJCZQBQC(ArrayList<HashMap<String, Object>> appList) {
		mAppList = appList;
	}

	public DelButtonAdapterJCZQBQC(Context c, ArrayList<HashMap<String, Object>> appList, int resource, String[] from, int[] to,String strZQWF) {
		mAppList = appList;
		mContext = c;
		ZQWF=strZQWF;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		keyString = new String[from.length];
		valueViewID = new int[to.length];
		System.arraycopy(from, 0, keyString, 0, from.length);
		System.arraycopy(to, 0, valueViewID, 0, to.length);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 if (mContext.getClass().equals(JcfootxhlbActivity.class)||(mContext.getClass().equals(JcBasketballxhlbActivity.class))) {
			if (convertView != null) {
				myFoot = (MyJcFoot) convertView.getTag();
			} else {
				convertView = mInflater.inflate(R.layout.buy_jczqbqcxhlb_list, null);
				myFoot = new MyJcFoot();
				myFoot.hteam = (TextView) convertView.findViewById(valueViewID[0]);
				myFoot.vteam = (TextView) convertView.findViewById(valueViewID[1]);
				myFoot.touzbu = (TextView) convertView.findViewById(valueViewID[2]);
				myFoot.buttonClose = (Button) convertView.findViewById(valueViewID[3]);//valueViewID[5]
				convertView.setTag(myFoot);
			}

			try {
				HashMap<String, Object> appInfo = mAppList.get(position);
				if (appInfo != null) {
					str_hteam = (String) appInfo.get(keyString[1]);
					str_vteam = (String) appInfo.get(keyString[2]);
//					str_sp = (String) appInfo.get(keyString[3]);
					str_touzhu= (String) appInfo.get(keyString[3]);
					int bid = (Integer) appInfo.get(keyString[4]);
					myFoot.hteam.setText(str_hteam);
					myFoot.vteam.setText(str_vteam);
//					System.out.println(str_hteam + "VS" + str_vteam + "----" + str_sp);
					if(str_touzhu.endsWith(",")){//去掉最后面的,
						myFoot.touzbu.setText(str_touzhu.substring(0, str_touzhu.length()-1));
					}else{
						myFoot.touzbu.setText(str_touzhu);
					}
					// myFoot.buttonClose.setBackgroundDrawable(myFoot.buttonClose.getResources().getDrawable(bid));
					myFoot.buttonClose.setOnClickListener(new jcButtonListener(position, myFoot.buttonClose.getId()));
				}
			} catch (Exception e) {
				// TODO: handle exception
//				System.out.println(e.toString());
			}
		}
		return convertView;
	}


	class jcButtonListener implements OnClickListener {
		private int position;
		private int btnId;

		jcButtonListener(int pos, int id) {
			position = pos;
			btnId = id;
		}

		@Override
		@SuppressWarnings("all")
		public void onClick(View v) {
			int vid = v.getId();
			if (vid == btnId) {
				if(!ZQWF.equals("BF")&&(!ZQWF.equals("SFC"))){
					if (mAppList.size() > 2) {
						removeItem(position);
						if (mContext.getClass().equals(JcfootxhlbActivity.class)) {
							((JcfootxhlbActivity) mContext).initBaseAdapter(mAppList);
						} /*else if (mContext.getClass().equals(bdxhlbActivity.class)) {
							((bdxhlbActivity) mContext).initBaseAdapter(mAppList);
						} */else if (mContext.getClass().equals(JcBasketballxhlbActivity.class)) {
							((JcBasketballxhlbActivity) mContext).initBaseAdapter(mAppList);
						}
					} else {
						Toast.makeText(mContext, R.string.select_at_least_two_game, Toast.LENGTH_SHORT).show();
					}
				}else{
					if (mAppList.size() > 1) {
						removeItem(position);
						if (mContext.getClass().equals(JcfootxhlbActivity.class)) {
							((JcfootxhlbActivity) mContext).initBaseAdapter(mAppList);
						} /*else if (mContext.getClass().equals(bdxhlbActivity.class)) {
							((bdxhlbActivity) mContext).initBaseAdapter(mAppList);
						} */else if (mContext.getClass().equals(JcBasketballxhlbActivity.class)) {
							((JcBasketballxhlbActivity) mContext).initBaseAdapter(mAppList);
						}
					} else {
						Toast.makeText(mContext, R.string.select_at_least_one_game, Toast.LENGTH_SHORT).show();
					}
				}
			}
		}
	}

	@Override
	public int getCount() {
		return mAppList.size();
	}

	@Override
	public Object getItem(int position) {
		return mAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void removeItem(int position) {
		if (mContext.getClass().equals(JcfootxhlbActivity.class)) {
			JcfootxhlbActivity.initMoney();
		} else if (mContext.getClass().equals(JcBasketballxhlbActivity.class)) {
			JcBasketballxhlbActivity.initMoney();
		}
//		JcfootxhlbActivity.initMoney();
		//如果用户点击删除操作非常快,可能会有数组下标越界异常
//		Log.v("removeItem", "position= "+position);
//		Log.v("removeItem", "mAppList.size()= "+mAppList.size());
		if(position>=mAppList.size()){
			return;
		}
		mAppList.remove(position);
		this.notifyDataSetChanged();
	}

	public ArrayList<ChildDTO> GetFootList() {
		ArrayList<ChildDTO> newlist = new ArrayList<ChildDTO>();
//		Log.i("GetFootList", "mAppList= "+mAppList.toString());
		for (int i = 0; i < mAppList.size(); i++) {
			String mId = (String) mAppList.get(i).get(keyString[0]);
			String str_hteam = (String) mAppList.get(i).get(keyString[1]);
			String str_vteam = (String) mAppList.get(i).get(keyString[2]);//buy_jcxhlb_list_vteam
			String str_touzhu = (String) mAppList.get(i).get(keyString[3]);//投注赛果
			Integer int_delbtn = (Integer) mAppList.get(i).get(keyString[4]);//del btn
			String str_sp = (String) mAppList.get(i).get(keyString[5]);//buy_jcxhlb_list_sp
			String str_time = (String) mAppList.get(i).get("tip_time");
//			 Log.d("DAda GetFootList", "keyString[0]= " + mId + " keyString[1]= " +
//					 str_hteam + " keyString[2]= " + str_vteam + " keyString[3]= " +str_touzhu +" keyString[4]= " + int_delbtn+ " keyString[5]= " + str_sp);
//			 Log.v("DAda GetFootList", "mId= " + mId + " str_hteam= " +
//			 str_hteam + " str_touzhu= " + str_touzhu + " str_sp= " + str_sp);
			ChildDTO childdto = new ChildDTO();
			childdto.mId = mId;
//			childdto.str_team = str_hteam;
			childdto.hTeam = str_hteam;
			childdto.vTeam = str_vteam;
			if(ZQWF.equals("BQC")){
				childdto.btnBQCcontent = str_touzhu;
//				childdto.bqc_selectsp=str_sp;
			}else if(ZQWF.equals("ZJQ")){
				childdto.selectZJQCG = str_touzhu;
//				childdto.bqc_selectsp=str_sp;
			}else if(ZQWF.equals("BF")){
				childdto.btnbfAllcontent = str_touzhu;
//				childdto.bqc_selectsp=str_sp;
			}else if(ZQWF.equals("SFC")){
				childdto.btnsfcAllcontent = str_touzhu;
//				childdto.bqc_selectsp=str_sp;
			}
			childdto.bqc_selectsp=str_sp;
			childdto.eTime = str_time;
			newlist.add(childdto);
		}
		return newlist;
	}

}